package cn.edu.zjut.mina.xcmahjong;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import cn.edu.zjut.mina.interf.ICardPile;

public class XcmjCardPile implements ICardPile {
	private List<XcmjCardBean> cardPile = new ArrayList<XcmjCardBean>();
	
	private XcmjCardBean[] cardPileArr = new XcmjCardBean[34 * 4];
	// 记录当前取到第几张牌
	private int currentTileIndex;
	
	public int getCurrentTileIndex() {
		return currentTileIndex;
	}

	public void setCurrentTileIndex(int currentTileIndex) {
		this.currentTileIndex = currentTileIndex;
	}

	// 随机因子
	private static Random random = new Random(System.currentTimeMillis());
	
	
	public int init() {

		// 清空牌堆
		cardPile.clear();
		XcmjCardBean cardBean = null;
		this.currentTileIndex = 0;

		for (int j = 1; j <= 9; j++) {
			//万
			for (int i = 1; i <= 4; i++) {
				cardBean = new XcmjCardBean();
				cardBean.setNumber(j);
				cardBean.setType('c');
				cardBean.setTypeInt(1);
				cardBean.setName();
				cardBean.setIndex(i);
				cardPile.add(cardBean);

				cardPileArr[(j-1)*4 + i - 1] = cardBean;
			}
			//筒
			for (int i = 1; i <= 4; i++) {
				cardBean = new XcmjCardBean();
				cardBean.setNumber(j);
				cardBean.setType('i');
				cardBean.setTypeInt(3);
				cardBean.setName();
				cardBean.setIndex(i);
				cardPile.add(cardBean);

				cardPileArr[36 + (j-1)*4 + i - 1] = cardBean;
			}
			//条
			for (int i = 1; i <= 4; i++) {
				cardBean = new XcmjCardBean();
				cardBean.setNumber(j);
				cardBean.setType('o');
				cardBean.setTypeInt(2);
				cardBean.setName();
				cardBean.setIndex(i);
				cardPile.add(cardBean);

				cardPileArr[72 + (j-1)*4 + i - 1] = cardBean;
			}
			
		}
		
		for (int j = 1; j <= 14; j+=2) {
			for (int i = 0; i < 4; i++)
			{
				//字
				cardBean = new XcmjCardBean();
				cardBean.setNumber(j);
				cardBean.setType('w');
				cardBean.setTypeInt(4);
				cardBean.setName();
				cardBean.setIndex(i+1);
				cardPile.add(cardBean);		

				cardPileArr[108 + ((j + 1)/2-1)*4 + i ] = cardBean;
			}
		}
		// 打乱1000次，约2毫秒
		this.disorganize(1000);
		
		return 0;
	}
	
	//打乱牌
	private void disorganize(int num) {
		for(int i = 0; i < num; i++) {
			int index1 = Math.abs(random.nextInt(34 * 4));
			int index2 = Math.abs(random.nextInt(34 * 4));
			XcmjCardBean cardBean3 = cardPileArr[index1];
			cardPileArr[index1] = cardPileArr[index2];
			cardPileArr[index2] = cardBean3;
		}
		//造牌，暗杠的形式
		for (int i = 0; i < 136; i++) {
			XcmjCardBean cardBean = cardPileArr[i];
			if(cardBean.getNumber() == 1 && cardBean.getTypeInt() == 1){
				if(i == 0 )
					break;
				XcmjCardBean cardBean3 = cardPileArr[0];
				cardPileArr[0] = cardPileArr[i];
				cardPileArr[i] = cardBean3;
				break;
			}
		}
		for (int i = 1; i < 136; i++) {
			XcmjCardBean cardBean = cardPileArr[i];
			if(cardBean.getNumber() == 1 && cardBean.getTypeInt() == 1){
				if(i == 1 )
					break;
				XcmjCardBean cardBean3 = cardPileArr[1];
				cardPileArr[1] = cardPileArr[i];
				cardPileArr[i] = cardBean3;
				break;
			}
		}
		for (int i = 2; i < 136; i++) {
			XcmjCardBean cardBean = cardPileArr[i];
			if(cardBean.getNumber() == 1 && cardBean.getTypeInt() == 1){
				if(i == 2 )
					break;
				XcmjCardBean cardBean3 = cardPileArr[2];
				cardPileArr[2] = cardPileArr[i];
				cardPileArr[i] = cardBean3;
				break;
			}
		}
		for (int i = 3; i < 136; i++) {
			XcmjCardBean cardBean = cardPileArr[i];
			if(cardBean.getNumber() == 1 && cardBean.getTypeInt() == 1){
				if(i == 52 )
					break;
				XcmjCardBean cardBean3 = cardPileArr[52];
				cardPileArr[52] = cardPileArr[i];
				cardPileArr[i] = cardBean3;
				break;
			}
		}
		
		
		for (int i = 3; i < 136; i++) {
			XcmjCardBean cardBean = cardPileArr[i];
			if(cardBean.getNumber() == 2 && cardBean.getTypeInt() == 1){
				if(i == 3 )
					break;
				XcmjCardBean cardBean3 = cardPileArr[3];
				cardPileArr[3] = cardPileArr[i];
				cardPileArr[i] = cardBean3;
				break;
			}
		}
		for (int i = 4; i < 136; i++) {
			XcmjCardBean cardBean = cardPileArr[i];
			if(cardBean.getNumber() == 2 && cardBean.getTypeInt() == 1){
				if(i == 4 )
					break;
				XcmjCardBean cardBean3 = cardPileArr[4];
				cardPileArr[4] = cardPileArr[i];
				cardPileArr[i] = cardBean3;
				break;
			}
		}
		for (int i = 5; i < 136; i++) {
			XcmjCardBean cardBean = cardPileArr[i];
			if(cardBean.getNumber() == 2 && cardBean.getTypeInt() == 1){
				if(i == 5 )
					break;
				XcmjCardBean cardBean3 = cardPileArr[5];
				cardPileArr[5] = cardPileArr[i];
				cardPileArr[i] = cardBean3;
				break;
			}
		}
		for (int i = 6; i < 136; i++) {
			XcmjCardBean cardBean = cardPileArr[i];
			if(cardBean.getNumber() == 2 && cardBean.getTypeInt() == 1){
				if(i == 6 )
					break;
				XcmjCardBean cardBean3 = cardPileArr[6];
				cardPileArr[6] = cardPileArr[i];
				cardPileArr[i] = cardBean3;
				break;
			}
		}
		
//		XcmjCardBean cardBean = null;
//		for (int i = 1; i <= 3; i++) {
//			cardBean = new XcmjCardBean();
//			cardBean.setNumber(1);
//			cardBean.setTypeInt(1);
//			cardBean.setName();
//			cardBean.setIndex(i);
//			cardPileArr[i] = cardBean;
//		}
//		cardBean = new XcmjCardBean();
//		cardBean.setNumber(1);
//		cardBean.setTypeInt(1);
//		cardBean.setName();
//		cardBean.setIndex(4);
//		cardPileArr[52] = cardBean;
	}
	
	public int size() {
		// TODO Auto-generated method stub
		return cardPile.size();
	}

	// 获取card
	public XcmjCardBean getCard() {
		// TODO Auto-generated method stub
//		int index = (int) (Math.random() * (cardPile.size()));
//		//System.out.println("index:" + index + "   size:" + cardPile.size());
//		XcmjCardBean cardBean = cardPile.get(index);
//		cardPile.remove(index);
//		//cardBean.typeToIndex();
//		return cardBean;
		XcmjCardBean cardBean = cardPileArr[currentTileIndex];
		currentTileIndex = currentTileIndex + 1;
		
		return cardBean;
	}

}
